package com.jeejam.product.dubbo;

import com.alibaba.dubbo.config.annotation.Service;
import com.jeejam.framework.base.pojo.Pager;
import com.jeejam.product.pojo.Product;
import com.jeejam.product.sdk.api.IProductService;
import com.jeejam.product.sdk.dto.ProductDto;
import com.jeejam.product.service.ProductService;
import com.jeejam.util.ShopBeanBuildUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by JACKEY on 2016/3/6.
 */
@Service(version = "1.0.0")
public class ProductDubboService implements IProductService{
    public ProductDubboService() {
        LOGGER.info("zzz ProductDubboService");
    }

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductDubboService.class);

    @Resource
    private ProductService productService;

    @Override
    public int counts() {
        return productService.counts();
    }

    @Override
    public Pager getPager(int currentPage, int pageSize) {
        return productService.getPager(currentPage, pageSize);
    }

    @Override
    public List<ProductDto> findAllByPage(int currentPage, int pageSize) {
        List<Product> productList = productService.selectAllByPage(currentPage, pageSize);
        return ShopBeanBuildUtil.slmCopyBeanList(productList, ProductDto.class);
    }

    @Override
    public List<ProductDto> findAll() {
        List<Product> productList = productService.findAll();
        return ShopBeanBuildUtil.slmCopyBeanList(productList, ProductDto.class);
    }

    @Override
    public Pager getPagerByCounts(int counts, int currentPage, int pageSize) {
        return productService.getPagerByCounts(counts, currentPage, pageSize);
    }

    @Override
    public void deleteById(int id) {
        productService.deleteById(id);
    }

    @Override
    public ProductDto getById(int id) {
        Product product = productService.findById(id);
        return ShopBeanBuildUtil.slmCopyBean(product, ProductDto.class);
    }

    @Override
    public void save(ProductDto productDto) {
        Product product = ShopBeanBuildUtil.slmCopyBean(productDto, Product.class);
        productService.save(product);
    }

    @Override
    public void update(ProductDto productDto) {
        Product product = ShopBeanBuildUtil.slmCopyBean(productDto, Product.class);
        productService.update(product);
    }

    @Override
    public int getTotalPage(int counts, int pageSize) {
        return productService.getTotalPage(counts, pageSize);
    }

    @Override
    public List<ProductDto> findByCategoryId(int rootCategoryId, int page, int pageSize) {
        List<Product> productList = productService.findByCategoryId(rootCategoryId, page, pageSize);
        return ShopBeanBuildUtil.slmCopyBeanList(productList, ProductDto.class);
    }

    @Override
    public int countsByCategoryId(int rootCategoryId) {
        return productService.countsByCategoryId(rootCategoryId);
    }

    @Override
    public List<ProductDto> findForNewByCategoryId(int rootCategoryId, int page, int pageSize) {
        List<ProductDto> result = null;
        try{
            List<Product> productList = productService.findForNewByCategoryId(rootCategoryId, page, pageSize);
            result = ShopBeanBuildUtil.slmCopyBeanList(productList, ProductDto.class);
        }catch (Exception e){
            LOGGER.error(Thread.currentThread().getStackTrace()[1].getClassName() + "|" + Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        }
        return result;
    }

    @Override
    public int countsForNewByCategoryId(int rootCategoryId) {
        return productService.countsForNewByCategoryId(rootCategoryId);
    }

    @Override
    public List<ProductDto> findForHotByCategoryId(int rootCategoryId, int page, int pageSize) {
        List<Product> productList = productService.findForHotByCategoryId(rootCategoryId, page, pageSize);
        return ShopBeanBuildUtil.slmCopyBeanList(productList, ProductDto.class);
    }

    @Override
    public int countsForHotByCategoryId(int rootCategoryId) {
        return productService.countsForHotByCategoryId(rootCategoryId);
    }

    @Override
    public List<ProductDto> findForRecommendByCategoryId(int rootCategoryId, int page, int pageSize) {
        List<Product> productList = productService.findForRecommendByCategoryId(rootCategoryId, page, pageSize);
        return ShopBeanBuildUtil.slmCopyBeanList(productList, ProductDto.class);
    }

    @Override
    public int countsForRecommendByCategoryId(int rootCategoryId) {
        return productService.countsForRecommendByCategoryId(rootCategoryId);
    }

    @Override
    public List<ProductDto> findByBrandId(int brandId, int page, int pageSize) {
        List<Product> productList = productService.findByBrandId(brandId, page, pageSize);
        return ShopBeanBuildUtil.slmCopyBeanList(productList, ProductDto.class);
    }

    @Override
    public List<ProductDto> findByBrandName(String brandName, int page, int pageSize) {
        List<Product> productList = productService.findByBrandName(brandName, page, pageSize);
        return ShopBeanBuildUtil.slmCopyBeanList(productList, ProductDto.class);
    }

    @Override
    public ProductDto getByCode(String productCode) {
        Product product = productService.getByCode(productCode);
        return ShopBeanBuildUtil.slmCopyBean(product, ProductDto.class);
    }

    @Override
    public List<ProductDto> findForRecommend(int page, int pageSize) {
        List<ProductDto> result = null;
        try{
            List<Product> productList = productService.findForRecommend(page, pageSize);
            result = ShopBeanBuildUtil.slmCopyBeanList(productList, ProductDto.class);
        }catch (Exception e){
            LOGGER.error(Thread.currentThread().getStackTrace()[1].getClassName() + "|" + Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        }
        return result;
    }

    @Override
    public List<ProductDto> findForNew(int page, int pageSize) {
        List<ProductDto> result = null;
        try{
            List<Product> productList = productService.findForNew(page, pageSize);
            result = ShopBeanBuildUtil.slmCopyBeanList(productList, ProductDto.class);
        }catch (Exception e){
            LOGGER.error(Thread.currentThread().getStackTrace()[1].getClassName() + "|" + Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> show(int userId, List<ProductDto> productDtoList) {
        List<Map<String, Object>> result = null;
        try{
            List<Product> productList = ShopBeanBuildUtil.slmCopyBeanList(productDtoList, Product.class);
            result = productService.show(userId, productList);
        }catch (Exception e){
            LOGGER.error(Thread.currentThread().getStackTrace()[1].getClassName() + "|" + Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        }
        return result;
    }

    @Override
    public List<ProductDto> findForBest(int page, int pageSize) {
        List<ProductDto> result = null;
        try{
            List<Product> productList = productService.findForBest(page, pageSize);
            result = ShopBeanBuildUtil.slmCopyBeanList(productList, ProductDto.class);
        }catch (Exception e){
            LOGGER.error(Thread.currentThread().getStackTrace()[1].getClassName() + "|" + Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        }
        return result;
    }

    @Override
    public List<ProductDto> findForHot(int page, int pageSize) {
        List<ProductDto> result = null;
        try{
            List<Product> productList = productService.findForHot(page, pageSize);
            result = ShopBeanBuildUtil.slmCopyBeanList(productList, ProductDto.class);
        }catch (Exception e){
            LOGGER.error(Thread.currentThread().getStackTrace()[1].getClassName() + "|" + Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        }
        return result;
    }

    @Override
    public ProductDto findById(int id) {
        ProductDto result = null;
        try{
            Product product = productService.findById(id);
            result = ShopBeanBuildUtil.slmCopyBean(product, ProductDto.class);
        }catch (Exception e){
            LOGGER.error(Thread.currentThread().getStackTrace()[1].getClassName() + "|" + Thread.currentThread().getStackTrace()[1].getMethodName(), e);
        }
        return result;
    }

    @Override
    public int countsForRecommend() {
        return productService.countsForRecommend();
    }

    @Override
    public int countsForNew() {
        return productService.countsForNew();
    }

    @Override
    public int countsForBest() {
        return productService.countsForBest();
    }

    @Override
    public int countsForHot() {
        return productService.countsForHot();
    }

    @Override
    public int getVotes(int id) {
        return productService.getVotes(id);
    }

    @Override
    public void updateViews(int id) {
        productService.updateViews(id);
    }

    @Override
    public void updateVotes(int votes, int id) {
        productService.updateVotes(votes, id);
    }

    @Override
    public List<Map<String, Object>> createExcelRecord(List<ProductDto> productDtoList) {
        List<Product> productList = ShopBeanBuildUtil.slmCopyBean(productDtoList, Product.class);
        return productService.createExcelRecord(productList);
    }

}
